package cn.acewill.yxd.yunxundian.ui.sign;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.PopupMenu;
import android.support.v7.widget.SearchView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.DatePicker;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import cn.acewill.yxd.yunxundian.BaseActivity;
import cn.acewill.yxd.yunxundian.R;
import cn.acewill.yxd.yunxundian.pubClass.MonPickerDialog;
import cn.acewill.yxd.yunxundian.utils.CacheActivity;
import cn.acewill.yxd.yunxundian.utils.DensityUtil;

/**
 * Created by xxx on 2018/1/11.
 * 评估主页
 */
public class MyEvaluationActivity extends BaseActivity implements  View.OnClickListener,SearchView.OnQueryTextListener,PopupMenu.OnMenuItemClickListener{

    private int FROM_WAHT = 1; //从门店签到 0 ，其他1；
    private List<Fragment> fragmentList = new ArrayList<>();
    private String[] tabNames = new String[]{"我的评估","他人评估"};
    public  int currentYear,currentMonth;
    private int thisyear,thismonth;
    private TextView titleText;
    private EvaluationForMeFragment meFragment;
    private EvaluationForOtherFragment otherFragment;
    public static String currentData;
    public static String searchText;
    private PageAapter adapter;
    private Fragment currentFragment;
    private FragmentManager manager;
    private TabLayout tabLayout;
    private SearchView search_bar;
    private int shopId = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_evaluation_view);
        CacheActivity.addActivity(this);
        initView();
    }
    private void initView(){
        FROM_WAHT = getIntent().getIntExtra("loadfrom",1);
        shopId = getIntent().getIntExtra("shopid",0);
        titleText = (TextView) findViewById(R.id.titleText);
        Toolbar toolbar = (Toolbar) findViewById(R.id.myToolBar);
        toolbar.setTitle("");
        setSupportActionBar(toolbar);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        if(getSupportActionBar() != null){
            getSupportActionBar().setDisplayShowTitleEnabled(false);
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        }
        currentData = BaseActivity.getMonth_Year();
        String[] res = currentData.split("-");
        currentYear = Integer.parseInt(res[0]);
        currentMonth = Integer.parseInt(res[1]);
        thismonth = currentMonth;
        thisyear = currentYear;
        titleText.setText(currentYear+"年"+currentMonth+"月");

        ImageView moreBtn = findViewById(R.id.moreBtn);
        moreBtn.setOnClickListener(this);
        ImageView schSelect = findViewById(R.id.schSelect);
        schSelect.setOnClickListener(this);
        search_bar = findViewById(R.id.search_bar);
        search_bar.setOnQueryTextListener(this);
     //   disPlayUnderLine(search_bar);
        meFragment = EvaluationForMeFragment.getInstance(FROM_WAHT,shopId);
        otherFragment = EvaluationForOtherFragment.getInstance(FROM_WAHT,shopId);
        fragmentList.add(meFragment);
        fragmentList.add(otherFragment);
        ViewPager mpager = (ViewPager) findViewById(R.id.viewPager);
        tabLayout = (TabLayout) findViewById(R.id.myTabs);
        tabLayout.addTab(tabLayout.newTab().setText(tabNames[0]));
        tabLayout.addTab(tabLayout.newTab().setText(tabNames[1]));
        tabLayout.setupWithViewPager(mpager);
        manager = getSupportFragmentManager();
        adapter = new PageAapter(manager);
        mpager.setAdapter(adapter);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.moreBtn:
                PopupMenu popupMenu = new PopupMenu(this, v);
                MenuInflater inflater = popupMenu.getMenuInflater();
                inflater.inflate(R.menu.menu_more, popupMenu.getMenu());
                popupMenu.setOnMenuItemClickListener(this);
                popupMenu.show();
                break;
            case R.id.schSelect:
                MonPickerDialog.OnDateSetListener listener = new MonPickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker startDatePicker, int startYear, int startMonthOfYear, int startDayOfMonth) {
                        currentData = startYear + "-";
                        if (startMonthOfYear + 1 < 10) {
                            currentData += "0" + (startMonthOfYear + 1);
                            titleText.setText(startYear+"年"+(startMonthOfYear + 1)+"月");
                        } else {
                            currentData += "" + (startMonthOfYear + 1);
                            titleText.setText(startYear+"年"+(startMonthOfYear+1)+"月");
                        }
                        refeshFragmentByDate();
                    }
                };
                MonPickerDialog mpd= new MonPickerDialog(this,listener,thisyear,thismonth,0);
                mpd.setTitle("请选择年月");
                mpd.show();


                break;

        }
    }

    @Override
    public boolean onMenuItemClick(MenuItem item) {
        switch (item.getItemId()){

            case R.id.lastMonth:
                if (currentMonth==1){
                    currentMonth = 12;
                    currentYear = currentYear-1;
                    currentData = currentYear+"-"+currentMonth;
                }else{
                    currentMonth = currentMonth -1;
                    if (currentMonth<10){
                        currentData = currentYear+"-0"+currentMonth;
                    }else{
                        currentData = currentYear+"-"+currentMonth;
                    }
                }
                titleText.setText(currentYear+"年"+currentMonth+"月");
                refeshFragmentByDate();

                break;
            case R.id.nowMonth:
                currentMonth = thismonth;
                currentYear = thisyear;
                if (currentMonth<10){
                    currentData = currentYear+"-0"+currentMonth;
                }else{
                    currentData = currentYear+"-"+currentMonth;
                }
                titleText.setText(currentYear+"年"+currentMonth+"月");
                refeshFragmentByDate();
                break;
            case R.id.nextMonth:
                if (currentMonth==12){
                    currentMonth = 1;
                    currentYear = currentYear+1;
                    currentData = currentYear+"-0"+currentMonth;
                }else{
                    currentMonth = currentMonth + 1;
                    if (currentMonth<10){
                        currentData = currentYear+"-0"+currentMonth;
                    }else{
                        currentData = currentYear+"-"+currentMonth;
                    }
                }
                titleText.setText(currentYear+"年"+currentMonth+"月");
                refeshFragmentByDate();
                break;
        }
        return false;
    }

    public void refeshFragmentByDate(){
        EvaluationForMeFragment.isRefresh = true;
        EvaluationForOtherFragment.isRefresh = true;
        int position = tabLayout.getSelectedTabPosition();
        currentFragment =  fragmentList.get(position);

        if (currentFragment != null){
            if (currentFragment instanceof EvaluationForMeFragment){
                meFragment.lazyLoad();
            }else{
                otherFragment.lazyLoad();
            }
        }
    }

    public void refeshFragmentBySearch(){
        EvaluationForMeFragment.searchRefresh = true;
        EvaluationForOtherFragment.searchRefresh = true;
        int position = tabLayout.getSelectedTabPosition();
        currentFragment =  fragmentList.get(position);

        if (currentFragment != null){
            if (currentFragment instanceof EvaluationForMeFragment){
                meFragment.lazyLoad();
            }else{
                otherFragment.lazyLoad();
            }
        }
    }

    @Override
    public boolean onQueryTextSubmit(String query) {
        return false;
    }

    @Override
    public boolean onQueryTextChange(String newText) {
        //传递参数
        searchText = newText;
        refeshFragmentBySearch();
        return false;
    }

    class PageAapter extends FragmentPagerAdapter {
        public PageAapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            Log.e("fragment",position+"");
            return fragmentList.get(position);
        }

        @Override
        public int getCount() {
            return fragmentList.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return tabNames[position];
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK){
            //不清空会有小问题，第二次进入会执行检索
            if (searchText!=null&&searchText.length()>0){
                searchText = "";
            }
            finish();
        }
        return super.onKeyDown(keyCode, event);
    }
}
